//
//  OrganChartViewController.m
//  SmartWatch
//
//  Created by zwj on 2022/11/28.
//

#import "OrganChartViewController.h"
#import <Charts/Charts-Swift.h>


@interface OrganChartViewController ()

@end

@implementation OrganChartViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isHideNavigationBar = YES;
    self.view.backgroundColor = BACKGROUNDCOLOR;
    
    
}


- (void)initSubviews {
    [super initSubviews];
    
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 180)];
    
    [backView az_setGradientBackgroundWithColors:@[[UIColor colorWithRGB:0xC4EFEC alpha:1.0],[UIColor colorWithRGB:0xC4EFEC alpha:0.75],[UIColor colorWithRGB:0xC4EFEC alpha:0]] locations:@[@0.0,@0.2,@1.0] startPoint:CGPointMake(0.5, 0) endPoint:CGPointMake(0.5, 1.0)];
    [self.view addSubview:backView];
    
    MyLinearLayout *navView = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Horz];
    navView.myHorzMargin = 0;
    navView.gravity = MyGravity_Vert_Center;
    navView.myHeight = 44;
    navView.myTop = StatusBarHeight;
    navView.padding = UIEdgeInsetsMake(0, 15, 0, 15);
    [self.view addSubview:navView];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setImage:UIImageMake(@"icon_arrow_left") forState:UIControlStateNormal];
    backBtn.mySize = CGSizeMake( 40, 40);
    backBtn.imageEdgeInsets = UIEdgeInsetsMake(10,10,10,10);
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:backBtn];
    
    
    MyLinearLayout *topLay = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Horz];
    topLay.myHorzMargin = 0;
    topLay.gravity = MyGravity_Vert_Center | MyGravity_Horz_Between;
    topLay.myHeight = 44;
    topLay.myTop = NavigationContentTop;
    topLay.padding = UIEdgeInsetsMake(0, 15, 0, 15);
    [self.view addSubview:topLay];
    
    {
        
        
        UILabel *content = [UILabel createLabelText:@"大脑检测趋势图" font:UIFontBoldMake(20) textColor:COLOR1515];
        content.mySize = kAutoSize;
        [topLay addSubview:content];
        
        QMUIButton * rightBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
        [rightBtn setImage:UIImageMake(@"arrow_down") forState:UIControlStateNormal];
        rightBtn.mySize = kAutoSize;
        [rightBtn setTitle:@"最近10次" forState:UIControlStateNormal];
        [rightBtn setTitleColor:HexColor(@"#454545") forState:UIControlStateNormal];
        rightBtn.titleLabel.font = UIFontMake(14);
        rightBtn.imagePosition = QMUIButtonImagePositionRight;
        rightBtn.spacingBetweenImageAndTitle = 5;
        rightBtn.myLeft = 10;
        [rightBtn addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [topLay addSubview:rightBtn];
        
    }
    
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, NavigationContentTop + 50, kScreenWidth, kScreenHeight - NavigationContentTop - 50)];
    scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:scrollView];
    
    MyLinearLayout *root = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
    root.myHorzMargin = 15;
    root.subviewVSpace = 15;
    [scrollView addSubview:root];
    
    NSArray *array = @[@"",@"",@"",@""];
    for (int i = 0; i < array.count; i++) {
        MyLinearLayout *item = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
        item.myHorzMargin = 0;
        item.padding = UIEdgeInsetsMake(15, 15, 15, 15);
        item.backgroundColor = COLOR_4;
        item.layer.cornerRadius = 6;
        item.subviewVSpace = 15;
        [root addSubview:item];
        
        {
            UILabel *name = [UILabel createLabelText:@"额叶区" font:UIFontBoldMake(16) textColor:COLOR1515];
            
            CGFloat width = [name sizeThatFits:CGSizeMake(name.preferredMaxLayoutWidth,CGFLOAT_MAX)].width;
            UIView *line = [UIView new];
            line.useFrame = YES;
            [line az_setGradientBackgroundWithColors:@[HexColor(@"#86F1E8"),HexColor(@"#ABE1FF")] locations:@[@0.0,@1.0] startPoint:CGPointMake(0, 0) endPoint:CGPointMake(1, 1)];
            [item addSubview:line];
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(item).offset(15);
                make.top.mas_equalTo(item.mas_top).offset(15+15);
                make.size.equalTo(@(CGSizeMake(width, 6)));
            }];
            
            name.mySize = kAutoSize;
            [item addSubview:name];
        }
        
        MyLinearLayout *chartsLay = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Horz];
        chartsLay.myHorzMargin = 0;
        chartsLay.myHeight = 200;
        [item addSubview:chartsLay];
        
        {
            MyLinearLayout *left = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
            left.myWidth = 20;
            left.myHeight = 200;
            left.gravity = MyGravity_Vert_Around | MyGravity_Horz_Center;
            left.padding = UIEdgeInsetsMake(0, 0, 15, 0);
            [chartsLay addSubview:left];
            
            NSArray *array = @[@"6",@"5",@"4",@"3",@"2",@"1"];
            for (int i = 0; i < array.count; i++) {
                UILabel *content = [UILabel createLabelText:array[i] font:UIFontMake(13) textColor:HexColor(@"#454545")];
                content.mySize = kAutoSize;
                [left addSubview:content];
            }
            
            MyLinearLayout *right = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
            right.weight  = 1;
            right.myHeight = 200;
            [chartsLay addSubview:right];
            
            {
                MyLinearLayout *xianLay = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
                xianLay.myHorzMargin = 0;
                xianLay.myHeight = 180;
                xianLay.gravity = MyGravity_Vert_Around;
                [right addSubview:xianLay];
                
                
                for (int i = 0; i < array.count + 1; i++) {
                    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth - 80, 0.5)];
//                    line.backgroundColor =  HexColor(i== array.count ? @"#454545" : @"#DADADA");
                    line.myHorzMargin = 0;
                    line.myHeight = 0.5;
                    [xianLay addSubview:line];
                    if(i== array.count)
                        line.backgroundColor = HexColor(@"#DADADA");
                    else
                        [self drawDashLine:line lineLength:6 lineSpacing:3 lineColor:HexColor(@"#DADADA")];
                }
                
                MyLinearLayout *bottom = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Horz];
                bottom.myHorzMargin = 0;
                bottom.myHeight = 20;
                bottom.gravity = MyGravity_Horz_Around | MyGravity_Vert_Top;
                [right addSubview:bottom];
                
                NSArray *array = @[@"03-25",@"03-25",@"03-25",@"03-25",@"03-25"];
                for (int i = 0; i < array.count; i++) {
                    UILabel *content = [UILabel createLabelText:array[i] font:UIFontMake(12) textColor:HexColor(@"#454545")];
                    content.mySize = kAutoSize;
                    [bottom addSubview:content];
                }
            }
        }
        
        MyLinearLayout *chartsValueLay = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Horz];
        chartsValueLay.myTop = -228;
        chartsValueLay.myHorzMargin = 0;
        chartsValueLay.myHeight = 200;
        chartsValueLay.padding = UIEdgeInsetsMake(0, 20, 20, 0);
        chartsValueLay.gravity = MyGravity_Horz_Around;
        [item addSubview:chartsValueLay];
        
        
        for (int i = 0; i < 5; i++) {
            
            MyLinearLayout *itemLay = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
            itemLay.myWidth = 16;
            itemLay.myVertMargin = 0;
            itemLay.subviewVSpace = 5;
            itemLay.gravity = MyGravity_Vert_Bottom | MyGravity_Horz_Center;
            [chartsValueLay addSubview:itemLay];
            
            
            
            UILabel *content = [UILabel createLabelText:[@(i+1) stringValue] font:UIFontMake(12) textColor:HexColor(@"#454545")];
            content.mySize = kAutoSize;
            [itemLay addSubview:content];
            
            UIView *view = [UIView new];
            view.myWidth = 16;
            view.myHeight = (i+1)*(180.0/7.0);
            view.backgroundColor = randomColor;
            view.layer.cornerRadius = 8;
            view.layer.maskedCorners = kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner;
            [itemLay addSubview:view];
        }
        
        
//        BarChartView *chatView = [[BarChartView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth - 60, 200)];
//        chatView.noDataText = @"暂无数据";
//        chatView.noDataFont = UIFontMake(20);
//        NSMutableArray *mutArray = @[].mutableCopy;
//        for (int j = 0; j < 6; j++) {
//            BarChartDataEntry *entry = [[BarChartDataEntry alloc]initWithX:j y:arc4random_uniform(100)];
//            [mutArray addObject:entry];
//        }
//        BarChartDataSet *dataSet = [[BarChartDataSet alloc]initWithEntries:mutArray label:@"图表"];
//        BarChartData *data = [[BarChartData alloc]initWithDataSet:dataSet];
//        chatView.data = data;
//        chatView.pinchZoomEnabled = NO;
//        chatView.doubleTapToZoomEnabled = NO;
//        chatView.clipDataToContentEnabled = YES;
//        chatView.legend.enabled = NO;
//        [item addSubview:chatView];
        
        
         
        
    }
}

- (void)rightBtnClick {
    
    QMUIAlertController *alertController = [QMUIAlertController alertControllerWithTitle:@"检测结果选择" message:@"" preferredStyle:QMUIAlertControllerStyleActionSheet];
    
    
    QMUIAlertAction *action1 = [QMUIAlertAction actionWithTitle:@"取消" style:QMUIAlertActionStyleCancel handler:^(QMUIAlertController *aAlertController, QMUIAlertAction *action) {
    }];
    [alertController addAction:action1];
    
    for (int i = 0; i < 4; i++) {
        QMUIAlertAction *action2 = [QMUIAlertAction actionWithTitle:@"最近10次检测结果" style:QMUIAlertActionStyleDefault handler:^(QMUIAlertController *aAlertController, QMUIAlertAction *action) {
        }];
        [alertController addAction:action2];
    }
    
    [alertController showWithAnimated:YES];
}

@end
